package com.efuture.io;

import java.io.*;

/**
 * @ClassNAME FileCopy
 * @Description 使用输入流 输出流 进行文件copy
 * @Author zhaoshanshan
 * @Date 2021/9/8 8:51 下午
 * @Version 1.0
 */
public class FileCopy {

    public static void main(String[] args) {
        //steramCopy();//流拷贝
        fileCopy();//FileReader FileWriter实现拷贝
    }
    private static void steramCopy(){//流拷贝
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try{
            fis = new FileInputStream("javasestudy/src/com.efuture.io/TestFileOne.txt");
            fos = new FileOutputStream("javasestudy/src/com.efuture.io/TestFileTwo.txt",true);
            byte[] bytes = new byte[1024];
            int readConunt = -1;
            while ((readConunt=fis.read(bytes))!=-1){
                fos.write(bytes,0,readConunt);
            }

            fos.flush();
        }catch (Exception e){
            e.getStackTrace();
        }finally {
            if(null!=fis){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null!=fos){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //使用FileReader和FileWriter拷贝
    private static void fileCopy(){
        FileReader fr = null;
        FileWriter fw = null;
        try{
           fr = new FileReader("javasestudy/src/com.efuture.io/TestFileOne.txt");
           fw = new FileWriter("javasestudy/src/com.efuture.io/TestFileFour.txt",true);
           char[] chars = new char[4];
           int readCount = -1;
           while ((readCount = fr.read(chars))!=-1){
               fw.write(chars,0,readCount);
           }
            fw.flush();
        }catch (Exception e){
            e.getStackTrace();
        }finally {
            try {
                if(null!=fr){
                    fr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(null!=fw){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}